package cn.jhc.teachingquality.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.jhc.teachingquality.R;
import cn.jhc.teachingquality.bean.Student;

/**
 * Created by luyanfei on 16-4-19.
 */
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentViewHolder> {

    private List<Student> students;
    private Context context;
    private TypedArray buttonsArray;

    public StudentAdapter(Context context, List<Student> students) {
        this.students = students;
        this.context = context;
        this.buttonsArray = context.getResources().obtainTypedArray(R.array.colored_buttons);
    }

    @Override
    public StudentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.student_item, parent, false);
        StudentViewHolder viewHolder = new StudentViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(StudentViewHolder holder, int position) {
        Student student = students.get(position);
        holder.studentNameView.setText(student.getName());
        Drawable background = buttonsArray.getDrawable(student.getRollStatus().ordinal() - 1);
        holder.studentNameView.setBackgroundDrawable(background);
        holder.studentNameView.setOnClickListener(new StudentClickListener(student));
    }

    @Override
    public int getItemCount() {
        return students.size();
    }

    public class StudentViewHolder extends RecyclerView.ViewHolder{
        TextView studentNameView;
        public StudentViewHolder(View itemView) {
            super(itemView);
            studentNameView = (TextView)itemView.findViewById(R.id.student_name_view);
        }
    }

    private class StudentClickListener implements View.OnClickListener{
        private Student student;

        public StudentClickListener(Student student) {
            this.student = student;
        }

        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    new ContextThemeWrapper(context,android.R.style.Theme_Holo_Light_Dialog));
            //LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //View dialogView = inflater.inflate(R.layout.check_roll_dialog,null);
            builder.setTitle(R.string.check_roll_dialog_title)
            .setItems(R.array.check_roll_states, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Student.RollStatus newStatus = Student.RollStatus.values()[which + 1];
                    student.setRollStatus(newStatus);
                    StudentAdapter.this.notifyDataSetChanged();
                }
            });
            builder.create().show();
        }
    }
}
